package models

import (
	"github.com/jinzhu/gorm"
	"kop/modules/money/entity"
	"time"
)

func AutoMigrate(dB *gorm.DB) {
	dB.AutoMigrate(new(entity.Money))
}

func FirstOrCreate(dB *gorm.DB, roleID, serverID int32) entity.Money {

	var money = entity.Money{
		RoleID:          roleID,
		ServerID:        serverID,
		Coin:            0,
		Income:          0,
		IncomeUpdatedAt: time.Now(),
	}

	if err := dB.FirstOrCreate(&money).Error; err != nil {
		panic(err)
	}
	if money.ServerID != serverID {
		money.ServerID = serverID
		if err := dB.Save(&money).Error; err != nil {
			panic(err)
		}
	}

	return money
}

// 在这里写那个根据时间自动补上自增长的银币
func GetMoney(dB *gorm.DB, roleID int32) entity.Money {

	var money = entity.Money{RoleID: roleID}
	if err := dB.First(&money).Error; err != nil {
		panic(err)
	}

	return money
}

func Save(dB *gorm.DB, money *entity.Money) error {

	return dB.Save(&money).Error
}
